package com.cc.Dao;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private static final Logger log = Logger.getLogger("BaseDao.class");
    //驱动
    private static String Driver = null;
    private static String Url = null;
    private static String User = null;
    private static String PWD = null;
    //数据库连接对象
    Connection conn = null;
    //指令操作对象
    PreparedStatement pst = null;

    //静态代码块
    static {
        //通过类加载器获取输入流
        String configFile = "database.properties";
        InputStream in = BaseDao.class.getClassLoader().getResourceAsStream(configFile);
        Properties prop = new Properties();
        try {
            prop.load(in);
            Driver = prop.getProperty("DRIVER");
            Url = prop.getProperty("URL");
            User = prop.getProperty("USER");
            PWD = prop.getProperty("PWD");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            //加载驱动
            Class.forName(Driver);
        } catch (ClassNotFoundException e) {
            log.error("加载驱动失败！");
            e.printStackTrace();
        }
    }
    public Connection getConnection() {
        try {
            conn = DriverManager.getConnection(Url,User,PWD);
            log.debug("获取数据库连接成功！");
        } catch (SQLException e) {
            log.error("获取数据库连接成功！");
            e.printStackTrace();
        }
        return conn;
    }
    //执行增删改方法
    public int executeUpdate(String sql, Object[] params) {
        //打开数据库连接
        conn = getConnection();
        //创建指令操作对象
        try {
            pst = conn.prepareStatement(sql);
            //设置参数
            if (params != null && params.length > 0) {
                for (int i = 0; i < params.length; i++) {
                    pst.setObject(i + 1, params[i]);
                }
            }
            log.debug("执行增删改操作开始，sql：" + sql);
            log.debug("操作成功！");
            //执行操作
            return pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(null);
        }
    }
    //执行查询方法
    public ResultSet executeQuery(String sql, Object[] params) {
        conn = getConnection();
        try {
            pst = conn.prepareStatement(sql);
            if (params != null && params.length > 0) {
                for (int i = 0; i < params.length; i++) {
                    pst.setObject(i + 1, params[i]);
                }
            }
            log.debug("执行查询操作，sql：" + sql);
            return pst.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //关闭数据库连接
    public void close(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pst != null) {
                pst.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
